<?php
/**
 +------------------------------------------------------------------------------
 * GiveAction控制器类
 +------------------------------------------------------------------------------
 * @category   App
 * @package  App
 * @subpackage  Action
 * @author   朝闻道 <Admin@iic.cn>
 * @version  $Id$
 +------------------------------------------------------------------------------
 */
defined ( "IICCMS" ) or exit ( "Access Denied" );
class GiveAction extends BaseAction{
	protected $vcw=array();
	function _initialize(){
		parent::_initialize ();
		$this->vcw= array(
				'guo999','edwin','adminwei','guojiacheng','caiwusmw','guan100221','zhangmei','szmei','zhangruizhuan','jianglei','kefu_gaofang','yhf1979'
		);
		// add by tb 暂时
		if (!in_array( $this->username, $this->vcw, true ) ) {
			$this->_ajm ( 300, '权限不足!', 0 );
			exit;
		}
	}
	/**
	 * 赠送账户信息汇总
	 */
	function index() {		
		$map=array();
		foreach ( $_REQUEST ['map'] as $key => $res ) {
			if (($res === '0' || $res > 0) || ! empty ( $res )) {
					$map [$key] = intval ( $res );
			}
		}
		
		//获取参数
		$maps=array();
		
		if($_REQUEST['time'] == 1){
			$time['stime'] = $_REQUEST['btime'];
			$time['etime'] = $_REQUEST['etime'];
			if($time['stime']) $condition['starttime'] = date('Y-m-d',$time['stime']);
			if($time['etime']) $condition['endtime'] = date('Y-m-d',$time['etime']);
			$maps = self::create_date($time,'updatetime',$maps);
		}else{
			$condition['starttime'] = $_REQUEST['starttime'];
			$condition['endtime'] = $_REQUEST['endtime'];
			$time['stime'] = gettime($_REQUEST['starttime']." 00:00:00");
			$time['etime'] = gettime($_REQUEST['endtime']." 23:59:59");
			$maps = self::create_date($time,'updatetime',$maps);
		}

		$status = $_REQUEST['status'];
		$condition['status'] = $status;
		if($status === '0' || $status === '1') $maps['status'] = $status;
						
		if($_REQUEST['moneytype']){
			$condition['moneytype'] = $_REQUEST['moneytype'];
			$maps['moneytype']=array("EQ",$_REQUEST['moneytype']);
		}		
		
		$isover = $_REQUEST['isover'];
		$condition['isover'] = $isover;
		if($isover === '0' || $isover === '1') $maps['isover'] = $isover;
		
		$maps['countoedu']=array('gt',0);
		
		$maps = parent::create_where($maps);
		
		$model = M ('UserGive');
		$count = $model->where($maps)->count ();
		$list=array();
		if ($count > 0) {
			$p = new Page ( $count, $this->listrows);
			$list = $model->where($maps)->order ($this->order)->limit ( $p->firstRow . ',' . $p->listRows )->select ();
		}
		$this->assign ( 'list', $list );
		$this->assign ('totalCount',$count);
		$userid=array();
		$user=M('User');
		foreach ($list as $v){
			$userid[]=$v['userid'];
		}
		$userid=implode(',',$userid);
		$userdata=$user->field('id,username')->where("id IN ({$userid})")->select();
		
		// 统计
		$d  = date( 'd', time() );
		$m  = date( 'm', time() );
		$y  = date( 'Y', time() );
		$w  = date( 'w', time() );
		$w1 = $w - 1;
		$w7 = 7-$w;
		$d2 = $d++;
		$date   = date( 'Ymd' );		// 今天日期
		$date2  = date( 'Ymd', strtotime("+1 day") );	// 明天日期
		$w1_ymd = date( 'Ymd', strtotime("-{$w1} day") ); // 周一日期
		$w7_ymd = date( 'Ymd', strtotime("+{$w7} day") ); // 周日日期
		$monty1 = date('Ym01');	// 本月第一天
		$monty0 = date('Ymd', strtotime("$monty1 +1 month -1 day"));	// 本月最后一天
		$monty2 = date('Ym01',strtotime("+1 month"));					// 下月第一天日期
		$monty3 = date('Ymd', strtotime("$monty1 +2 month -1 day"));	// 下月最后一天
		//echo $date.'--'.$date2.'--'.$w1_ymd.'--'.$w7_ymd.'--'.$monty1.'--'.$monty0.'--'.$monty2.'--'.$monty3;
		$count_today = $count_tomorroy = $count_week = $count_monty = $count_prox = 0;
		// 对校日期
		$sta_tmp = M( 'user_give' )->where( "`status`=1 and countoedu>0 and moneytype='{$moneytype}'
			and (qishu-(({$y}-FROM_UNIXTIME(startday,'%Y'))*12+{$m}-FROM_UNIXTIME(startday,'%m')))>-2" )->select();
		foreach ( $sta_tmp as $value) {
			$date_arr  = array();
			$date_arr[] = date( 'Ymd', $value['startday'] );
			for ( $i=1; $i<$value['qishu']; $i++ ) {
				$date_arr[] = date( 'Ymd', strtotime("+{$i} month", $value['startday']) );
			}
			if ( in_array( $date,$date_arr ) ) $count_today += $value['eachoedu'];
			if ( in_array( $date2,$date_arr ) ) $count_tomorroy += $value['eachoedu'];
			//if ( $value['id'] == 737 ) dump($date_arr);
			foreach ( $date_arr as $v ) {
				//if ( $v == $date2 ) echo $v;
				if ( $v>=$w1_ymd && $v<=$w7_ymd ) $count_week  += $value['eachoedu'];
				if ( $v>=$monty1 && $v<=$monty0 ) $count_monty += $value['eachoedu'];
				if ( $v>=$monty2 && $v<=$monty3 ) $count_prox  += $value['eachoedu'];
			}
		}
		$statistics = array(
			'count_today'    => $count_today,
			'count_tomorroy' => $count_tomorroy,
			'count_week'     => $count_week,
			'count_monty'    => $count_monty,
			'count_prox'     => $count_prox,
		);
		$this->assign('userdata',field2key($userdata, 'id'));
		$this->assign('condition',$condition);
		$this->assign('statistics',$statistics);
		$this->assign('moneytype',$moneytype);
		$this->display ();
	}//end index
	
	/**
	 * 查看返款明细
	 */
	function logs() {
		$where=array();
		$only=$_REQUEST['only'];
		$userid=$_GET['userid'];
		$auth=$_GET['auth'];
		if($only){
			$where['only']=$only;
		}
		if($userid){
			$where['userid']=$userid;
		}
		if($auth){
			$where['auth']=$auth;
		}
		
		//获取参数
		$keyword = $_REQUEST["keyword"];
		$searchtype = $_REQUEST['searchtype'];
		$condition['keyword'] = $keyword;
		$condition['searchtype'] = $searchtype;
		if($keyword && $searchtype) $where[$searchtype] = $keyword;
		
		$mon = $_REQUEST['mon'];
		$condition['mon'] = $mon;
		if($mon) $where['mon'] = $mon;			
		
		$model = M ('UserGiveLogs');
		$count = $model->where($where)->count ();
		$list=array();
		if ($count > 0) {
			$p = new Page ( $count, $this->listrows);
			$list = $model->where($where)->order ($this->order)->limit ( $p->firstRow . ',' . $p->listRows )->select ();
		}
		$this->assign ( 'list', $list );
		$this->assign ('totalCount',$count);
		
		$userid=array();
		$user=M('User');
		foreach ($list as $v){
			$userid[]=$v['userid'];
		}
		$userid=implode(',',$userid);
		$userdata=$user->field('id,username')->where("id IN ({$userid})")->select();
		$this->assign('userdata',field2key($userdata, 'id'));
		$this->assign('where',$where);
		$this->assign('keyword',$keyword);
		$this->display ();
	}
}



